<template>
  <el-dialog :title='title'
  :visible="visible"
@close="closeFn"
@open='openFn'
  >
    <el-form :model="form" label-width="120px" ref="form">
      <el-form-item label="区域名称">
        <el-input
          style="width: 396px"
         v-model="form.regionName" autocomplete="off"
         placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="备注说明">
        <el-input
          style="width: 396px"
              type="textarea"
            show-word-limit
              maxlength="40"
              :rows="3"
        v-model="form.remark" autocomplete="off"
        placeholder="请输入备注(不超过40字)"></el-input>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="start" align="center">
          <el-col :span="18">
            <el-button class="closeFn" @click="closeFn">取 消</el-button>
            <el-button
              style="margin-left: 20px"
              class="comfirm"
              @click="confirmFn"
              >确 定</el-button
            >
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addArea, editAreaList } from '@/api/point'
export default {
  props: {
    visible: Boolean,
    rows: {
      type: [Array, Object],
      default: () => {}
    },
    or: {
      type: Number,
      default: 1
    },
    queryParams: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      form: {
        regionName: '',
        remark: ''
      },
      title: '新增区域'

    }
  },
  created () {
    if (this.or === 2) {
      console.log(this.rows)
      this.title = '修改区域'
      this.form = {
        regionName: this.rows.name,
        remark: this.rows.remark
      }
    }
  },
  methods: {
    openFn () {
      console.log(this.or)
    },
    closeFn () {
      this.$emit('update:visible', false)
    },
    async confirmFn () {
      this.$refs.form.validate()
      if (this.or === 1) {
        try {
          await addArea({ ...this.form })
          //   console.log(res)
          this.$message.success('添加成功')
          this.$parent.getPointListFn()
          this.$refs.form.resetFields()
        } catch (error) {
          this.$message.error(error.response.data)
        } finally {
          this.closeFn()
        }
      } else if (this.or === 2) {
        try {
          await editAreaList({ ...this.form, id: this.rows.id })
          this.$message.success('修改成功')
          this.closeFn()
          this.getPointListFn()
        } catch (error) {
          this.$message.error(error.response.data)
        }
      }
    }
  },
  inject: ['getPointListFn']
}
</script>

<style lang="less" scoped>

/deep/ .el-dialog {
    width: 630px;
    border-radius: 15px;
}
/deep/ .el-dialog__header {
  padding: 20px 20px 10px;
  text-align: left;
  font-weight: 700;
  font-size: 16px;
}
/deep/ .el-dialog__body {
  padding: 20px 20px;
}
.cancel {
  width: 80px;
  height: 36px;
  color: #655b56 !important;
  background-color: antiquewhite;
  border: none;
}
.comfirm {
  width: 80px;
  height: 36px;
  color: #fff;
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: none;
}
</style>
